package com.study.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.mdao.OpportunityMapper;
import com.study.model.mdao.OrderFormDetailMapper;
import com.study.model.mdao.OrderFormMapper;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Linkman;
import com.study.services.AddresdetailService;
import com.study.services.CustomerService;
import com.study.services.DictionaryService;
import com.study.services.GoodsService;
import com.study.services.MoneyBackPlanService;
import com.study.services.OrderService;
import com.study.services.OrderfromServices;
import com.study.services.UserService;

/**
 * 退货
 * @author 郭星
 *
 */
@Controller
public class OrderReturnController {

	@Autowired
	OrderService service;

	@Autowired
	UserService userservice;
	@Autowired
	DictionaryService dicservice;
	@Autowired
	AddresdetailService addresdetailService;
	@Autowired
	CustomerService customerservice;
	@Autowired
	GoodsService goodsService;
	@Autowired
	MoneyBackPlanService moneybackplanservice;
	@Autowired
	OpportunityMapper oppmapper;
	@Autowired
	OrderFormDetailMapper odmap;
	@Autowired
	OrderfromServices orderfromServices;

	/**
	 * 访问地址http://localhost:8089/goods
	 * 
	 * @return
	 *
	 */
	@RequestMapping("returnGoods")
	public String first(HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		return "onSale/returnsale";
	}
	

	/**
	 * 传入ifram表单信息
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/returnFrames")
	public ModelAndView first2(HttpSession session) {
		ModelAndView mv = new ModelAndView("/onSale/tuihuodilog");
		System.out.println("orderForm:iframe");
		return mv;
	}
	
	
	/**
	 * 分页查询
	 */
	@PostMapping("/onSale/returnItem")
	@ResponseBody
	public String getForm1(HttpSession session) {
		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = service.selectByPagerSqlReturn("", "", "", 1, 10, uid);
		formatDate(list);
	
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		String json = JSONObject.toJSONString(pager);

		return json;
	}
	
	/**
	 * oreder分页、全部、带条件查询
	 * 
	 * @param selects     搜索框条件头
	 * @param selectinput 搜索框条件中
	 * @param selectend   搜索框条件尾
	 * @param currentPage 当前页
	 * @param pagesize    页大小
	 * @return
	 */
	@PostMapping("/onSale/returnItem2sd")
	@ResponseBody
	public String getForm3(String selects, String selectinput, String selectend, int currentPage, int pagesize,
			HttpSession session) {
		System.out.println(currentPage + "currentPage");
		System.out.println(pagesize + "pagesize");
		System.out.println(selectinput);
		System.out.println(selects);
		if (selects.equals("全部数据")) {
			selects = null;
		}

		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = service.selectByPagerSqlReturn(selects, selectinput, selectend, currentPage, pagesize, uid);
		formatDate(list);
		
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		String json = JSONObject.toJSONString(pager);

		return json;
	}
	
	/**
	 * 处理list集合中的时间格式
	 * 
	 * @param list
	 */
	public static void formatDate(List<JSONObject> list) {

		int i = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		for (JSONObject jsonObject : list) {
			System.out.println(jsonObject);
			String string = jsonObject.getString("rtimes");
			if (string!=null&&string.length()>0) {
				try {

					Date parse = format.parse(string);
					System.out.println(format.format(parse));
					list.get(i).put("rtimes", format.format(parse).toString());
					i++;
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
		}
	}
	
	
	
	/**
	 * 通过名称查询对应客户
	 * 及订单信息
	 * 
	 * @return
	 * @return
	 * 
	 * @return
	 */
	@PostMapping("/onSale/findByCoustomerNameAndOrder")
	@ResponseBody
	public JSONObject findByCoustomer(Integer custid, HttpSession session) {
		System.out.println(custid);
		List<Linkman> selectByName = customerservice.selectById(custid);
		JSONObject jso = new JSONObject();
		jso.put("custo", selectByName);// 放入联系人
		jso.put("order", service.findOrderByCus(custid));
		return jso;
	}
	
	
	/**
	 * 通过订单id查询商品信息
	 * 
	 * 
	 * @return
	 * @return
	 * 
	 * @return
	 */
	@PostMapping("/onSale/findByCoustomerNameAndOrderS")
	@ResponseBody
	public JSONObject findByOrderGoodsCS(Integer oid, HttpSession session) {
		System.out.println(oid);
		JSONObject jso = new JSONObject();
		jso.put("goodsitem", odmap.findTextByOid(oid));// 放入 商品
		return jso;
	}
	
	/**
	 * 添加退货记录
	 * @param custid
	 * @param orderid
	 * @param gid
	 * @param users
	 * @param quatity
	 * @param havetime
	 * @param doback
	 * @param desc
	 * @return
	 */
	@PostMapping("addTuihuo")
	@ResponseBody
	public String addForm(String custid, String orderid, String gid, String users, String quatity,
			Date havetime, String doback, String desc, HttpSession session) {
		System.out.println("客户"+custid+" 订单"+orderid+" 商品"+gid+" 负责人"+users+" 数量"+quatity+" 时间"+havetime+" 原因"+doback+" 详情"+desc);
		String result="";
		//开始添加退货记录
		orderfromServices.addReturnGoods(custid, orderid, gid, users, quatity, havetime, doback, desc);
		return result + "";
	}
	
	

	/**
	 * 通过订单id查询商品可退货最大值
	 * 
	 * 
	 * @return
	 * @return
	 * 
	 * @return
	 */
	@PostMapping("/onSale/findByReturnMax")
	@ResponseBody
	public JSONObject findByReturnMax(Integer oid,Integer gid, HttpSession session) {
		System.out.println(oid+" "+gid);
		JSONObject jso = new JSONObject();
		int findGoodReturnMax = odmap.findGoodReturnMax(oid,gid);
		System.out.println(findGoodReturnMax);
		jso.put("goodsmax", findGoodReturnMax);// 放入 商品可退货最大值
		return jso;
	}
	
}
